package com.easyExcel.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.easyExcel.dto.User;
import org.junit.jupiter.api.Test;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author：万意佳
 * @Date:
 * @Description:
 **/
@RestController
@RequestMapping("/excel")
public class ReadExcel {


    @Test
    public void testReadExcel() {
        // 读取的excel文件路径"
        String filename = "D:\\test\\Excel\\user1.xlsx";
        // 读取excel
        EasyExcel.read(filename, User.class, new AnalysisEventListener<User>() {
            // 每解析一行数据,该方法会被调用一次
            @Override
            public void invoke(User user, AnalysisContext analysisContext) {
                System.out.println("解析数据为:" + user.toString());
            }
            // 全部解析完成被调用
            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                System.out.println("解析完成...");
                // 可以将解析的数据保存到数据库
            }
        }).sheet().doRead();
    }

}
